﻿{% extends "gma/base.html" %}

<!-- CONTENT
  =====================================================================-->

{% block content %}

<div class="page__hd">
    <h1 class="page__title">
        <i class="fa fa-broadcast-tower weui_grid_icon_fa" style="font-size: 32px"> </i> 广播消息
    </h1>
    <p class="page__desc"> 目标: {{ category_desp }} </p>
    <p class="page__desc"> 数量: {{ user_count }} </p>
</div>

<div class="page__bd">
    <div class="weui-cells__title">消息内容</div>
    <div class="weui-cells weui-cells_form">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <textarea class="weui-textarea" id="push_message" placeholder="请输入要推送给用户的消息内容" rows="10"></textarea>
            </div>
        </div>
    </div>
</div>

<div class="weui-btn-area">
    <a class="weui-btn weui-btn_block weui-btn_primary" href="javascript:pushMessage(1)"> <i class="fa fa-bullhorn"></i> 推送消息 </a>
    {% if permissions.system_config.open %}
    <a class="weui-btn weui-btn_block weui-btn_primary" href="javascript:pushMessage(0)"> <i class="fa fa-bullhorn"></i> 推送测试消息 </a>
    {% endif %}
</div>

<div class="page__ft">
    <a href="{% url 'gma:home' %}">
        <span class="bottom-logo">
           HiBao
       </span>
    </a>
</div>

{% endblock content %}

<!-- SCRIPT
  =====================================================================-->

{% block extra_js %}
<script>

function pushMessage(apns_production) {
    const message = $('#push_message').val();
    if (!message) {
        alert("发送的内容不可以为空！");
        return;
    }

    // 编辑消息时，页面会自动滚动，导致弹框无法被点击
    document.activeElement.scrollIntoViewIfNeeded();

    show_confirm_dialog('广播消息', '你确定给{{ category_desp }}发送消息吗?', ()=> {
        let url = "{% url 'gma:api_push_message' %}";
        let data = {
            'category': {{ category }},
            'uid': '{{ uid }}',
            'message': message,
            'apns_production': apns_production,
        };

        new my_ajax(url, data, '', '', () => {
            show_toast(`推送成功!`);
            setTimeout(() => {
                window.location.reload();
            }, 1000);
        });
    });
}

</script>
{% endblock extra_js %}